"use strict";
const code = require("../../errorCode.js");
const Controller = require("egg").Controller;

class HomeCtrl extends Controller {
  async send() {
    const { ctx, app } = this;
    const topic = ctx.params.topic;
    const msg = JSON.stringify(ctx.request.body);
    try {
      if (topic != "") {
        await app.emqtt.publish(topic, msg, { qos: 0 });
        ctx.body = app.done();
      } else {
        ctx.body = app.error(code.MQ_NO_TOPIC,'topic缺失或为空！');
      }
    } catch (e) {
      app.logger.error(e);
      ctx.body = app.error(code.MQ_ERR,'mqtt发生未知错误！'+e.message);
    }
  }
}

module.exports = HomeCtrl;
